#include "logger_builder.hpp"
#include "logger_factory.hpp"


LoggerBuilder::LoggerBuilder() {
    m_config.console.enabled = false;
    m_config.file.enabled = false;
    m_config.network.enabled = false;
}


LoggerBuilder& LoggerBuilder::withLogLevel(LogLevel level) {
    m_config.level.default_level = level;
    return *this;
}


LoggerBuilder& LoggerBuilder::withConsoleSink(bool apply_filtering) {
    m_config.console.enabled = true;
    m_config.console.apply_filtering = apply_filtering;

    if (apply_filtering) {
        m_config.filter.enabled = true;
    }
    return *this;
}


LoggerBuilder& LoggerBuilder::withFileSink(const std::string& filename, size_t max_size, size_t max_files) {
    m_config.file.enabled = true;
    m_config.file.filename = filename;
    m_config.file.max_size = max_size;
    m_config.file.max_files = max_files; 
    return *this;
}


LoggerBuilder& LoggerBuilder::withNetworkSink(const std::string& url, long timeout, long connect_timeout) {
    m_config.network.enabled = true;
    m_config.network.url = url;
    m_config.network.timeout = timeout;
    m_config.network.connect_timeout = connect_timeout;
    return *this;
}


LoggerBuilder& LoggerBuilder::withFilter(const std::string& pattern, const std::string& replacement) {
    m_config.filter.enabled = true;
    
    if (!m_config.filter.default_filters_cleared) {
        m_config.filter.filters.clear();
        m_config.filter.default_filters_cleared = true;
    }
    m_config.filter.filters.emplace_back(pattern, replacement);
    return *this;
}


LoggerBuilder& LoggerBuilder::clearFilters() {
    m_config.filter.filters.clear();
    m_config.filter.default_filters_cleared = true;
    return *this;
}


LoggerBuilder& LoggerBuilder::withJsonFormat() {
    m_config.formatter.type = FormatterConfig::Type::JSON;
    return *this;
}


LoggerBuilder& LoggerBuilder::withBasicFormat() {
    m_config.formatter.type = FormatterConfig::Type::Basic;
    return *this;
}


LoggerBuilder& LoggerBuilder::enableBacktrace(size_t size) {
    m_config.backtrace.enabled = true;
    m_config.backtrace.size = size;
    return *this;
}


std::shared_ptr<ILogger> LoggerBuilder::build() {
    return LoggerFactory::create(m_config);
}

